<?php
namespace Admin\Model;
use Think\Model;
class RegionModel extends Model 
{
	protected $insertFields = array('parent_id','region_name','region_type','agency_id');
	protected $updateFields = array('region_id','parent_id','region_name','region_type','agency_id');
	protected $_validate = array(
		array('parent_id', 'number', '父级id必须是一个整数！', 2, 'regex', 3),
		array('region_name', '1,120', '地区名称的值最长不能超过 120 个字符！', 2, 'length', 3),
		array('region_type', 'number', '必须是一个整数！', 2, 'regex', 3),
		array('agency_id', 'number', '必须是一个整数！', 2, 'regex', 3),
	);
	public function search($pageSize = 20)
	{
		/**************************************** 搜索 ****************************************/
		$where = array();
		if($parent_id = I('get.parent_id'))
			$where['parent_id'] = array('eq', $parent_id);
		if($region_name = I('get.region_name'))
			$where['region_name'] = array('like', "%$region_name%");
		if($region_type = I('get.region_type'))
			$where['region_type'] = array('eq', $region_type);
		if($agency_id = I('get.agency_id'))
			$where['agency_id'] = array('eq', $agency_id);
		/************************************* 翻页 ****************************************/
		$count = $this->alias('a')->where($where)->count();
		$page = new \Think\Page($count, $pageSize);
		// 配置翻页的样式
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$data['page'] = $page->show();
		/************************************** 取数据 ******************************************/
		$data['data'] = $this->alias('a')->where($where)->group('a.region_id')->limit($page->firstRow.','.$page->listRows)->select();
		return $data;
	}
	// 添加前
	protected function _before_insert(&$data, $option)
	{
	}
	// 修改前
	protected function _before_update(&$data, $option)
	{
	}
	// 删除前
	protected function _before_delete($option)
	{
		if(is_array($option['where']['region_id']))
		{
			$this->error = '不支持批量删除';
			return FALSE;
		}
	}
	/************************************ 其他方法 ********************************************/
}